function [protocol, err] = get_protocol(filename)
%
% Function to read information from new data file structure (mat files), and return
% as protocol structure used to generate the note list in the datac analysis program
%
% 10/15/2000
% Paul B. Manis, Ph.D.
% pmanis@med.unc.edu
%

protocol = [];
err =1 ; % indicate error unless all is right
if(nargin ~= 1 & ~ischar(filename))
   fprintf(2, 'get_protocol: bad calling argument?\n');
   return;
end;

if(nargout ~= 2)
   fprintf(2, 'get_protocol: requires 2 output arguments');
   return;
end;


% build the components of the structure

protocol.etime=[];		%the starting time of the protocol in "real time" (read from notefile)
protocol.proto=[];		%the name of the protocol
protocol.mode=[];		% the type of data acquisition that was done
protocol.seq=[];		%the sequence that was to be run
protocol.frec=[];		%the first record in the protocol
protocol.lrec=[];		%the last record in the protocol
protocol.ngrp=[];		% the number of records in each "group" (data, take)
protocol.ztime=[];		%the starting time of the protocol in ztime ( set to 0 here; can be set later when data file is read)
protocol.valve=[];		%The valve setting (initialized at 1)
protocol.notes=[];		%the concatenation of note entries since the last protocol

v=[];
v = load(filename, 'Index');
if(isempty(v))
   fprintf(2, 'No index in file %s\n', filename);
   return;
end;
if(isempty(fieldnames(v)))
   fprintf(2, 'No index in file %s\n', filename);
   return;
end;
% all the information we really need can be gleaned from the index, 
% although we will need to read the STIM blocks to get some info.
%
INDEX = v.Index; %.INDEX;
% we will collapse the entries by block 
x=unique([INDEX(:).block]); % find the actual block numbers in the index
pindx = 1;
for i = 1:length(x) % through all the entries
   blk = x(i);
   % load the protocol from the associated sf file
   
   [sfile, dfile, notes] = block_info(filename, blk);
   if(~isempty(sfile) & ~isempty(fieldnames(sfile)))
      protocol(pindx).proto = sfile.Name.v;
      protocol(pindx).seq = sfile.Sequence.v;
      protocol(pindx).frec = dfile.Record;
      y=find([INDEX(:).block] == blk); % find all the records in the block
      protocol(pindx).lrec = max([INDEX(y).record])-1; % find the last record
      protocol(pindx).etime = INDEX(y(1)).time; % find the first time associated with block
      protocol(pindx).ztime = 0;
      protocol(pindx).valve = 1;
      protocol(pindx).mode = dfile.Data_Mode.v;
      protocol(pindx).ngrp = 1;
      if(~isempty(notes))
         for j = 1:size(notes{1}.v, 1)
            c=char(notes{1}.v(j,:));
            if(strmatch(fieldnames(notes{1}), 't'))
               %  fprintf(1, '\nNote (%2d:%2d:%2.0f):\n', notes{1}.t(4), notes{1}.t(5), notes{1}.t(6))
            else
               %  fprintf(1, '\nNote:\n');
            end;
            protocol(pindx).notes = [];
            for k = 1:size(c,1) 
               cl = unblank(c(k,:));
               if((length(cl) > 0) && cl(end) ~= '.')
                  cl = [cl '.'];
               end;
               protocol(pindx).notes = [protocol(pindx).notes ' ' sprintf(' %s', cl)];
            end;
         end;
      else         
         protocol(pindx).notes = []; % no note in the section...
      end;
      pindx = pindx + 1;
   end;
end;
err = 0;
return;

function [sfile, df, notes] = block_info(filename, wblk)

sfile=[];
df=[];
notes={};
sfc = sprintf('sf%d', wblk);
warning off;
sfile = load(filename, sfc); % read the stim block
warning on;
if(~isempty(fieldnames(sfile)))
   sfile = eval(['sfile.' sfc]);
   dfc = sprintf('df%d', wblk);
   df = load(filename, dfc); % read the data block
   df = eval(['df.' dfc]);
else
   sfile = [];
   df = [];
end;
nfc = sprintf('Note%d_*', wblk);
warning off;
nf = load(filename, nfc);
warning on;
if(~isempty(fieldnames(nf)))
   fn = fieldnames(nf);
   for i = 1:length(fn)
      notes{i}.v = eval(['nf.' fn{i} '.v']);
      %      notes{i}.t = eval(['nf.' fn{i} '.t']);
   end;
end;
return;
